package jmine.tec.web.wicket.component.command.img;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.grid.DataGridView;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.IModel;

/**
 * @author Rafael Volpato
 * @date Aug 2, 2011
 */
public class ImageCommandsPanel extends Panel {

    private final List<ImageCommand> commands;

    /**
     * Construtor
     * 
     * @param id id do painel
     * @param commandParam commandParam
     * @param commands lista de comandos que deve ser exibidos no painel
     */
    public ImageCommandsPanel(String id, Serializable commandParam, List<ImageCommand> commands) {
        super(id);
        this.commands = commands;
        ListDataProvider<Serializable> dataProvider = new ListDataProvider<Serializable>(Collections.singletonList(commandParam));
        this.add(new DataGridView<Serializable>("commands", this.getCellPopulators(), dataProvider));
    }

    /**
     * Returns the index of the command that is an instance of the given class
     * 
     * @param commandClass commandClass
     * @return index
     */
    public int getCommandIndex(Class<? extends ImageCommand> commandClass) {
        for (int i = 0; i < this.commands.size(); i++) {
            if (commandClass.isInstance(this.commands.get(i))) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Cria novas instancias dos {@link ICellPopulator} necessarios.
     * 
     * @return lista de {@link ICellPopulator}
     */
    private List<ICellPopulator<Serializable>> getCellPopulators() {
        List<ICellPopulator<Serializable>> cols = new ArrayList<ICellPopulator<Serializable>>(this.commands.size());
        for (ImageCommand command : this.commands) {
            cols.add(new CommandCellPopulator(command));
        }
        return cols;
    }

    /**
     * {@link ICellPopulator} da sub-tabela de comandos.
     * 
     * @author Rafael Volpato
     * @date Aug 2, 2011
     */
    private class CommandCellPopulator implements ICellPopulator<Serializable> {

        private ImageCommand command;

        /**
         * Construtor.
         * 
         * @param command command
         */
        public CommandCellPopulator(ImageCommand command) {
            this.command = command;
        }

        /**
         * {@inheritDoc}
         */
        public void populateItem(Item<ICellPopulator<Serializable>> cellItem, String componentId, final IModel<Serializable> rowModel) {
            AbstractLink link = this.command.createLinkComponent(componentId, rowModel.getObject());
            link.add(new Image("commandImage", this.command.getImage()));
            link.setMarkupId(this.command.getMarkupIdPrefix() + "_" + rowModel.getObject());
            link.setOutputMarkupId(true);
            cellItem.add(link);
        }

        /**
         * {@inheritDoc}
         */
        public void detach() {
            // vazio
        }
    }
}
